<?php


namespace backend\controllers;


use common\helpers\commonApi;
use common\models\Campus;
use common\models\CampusAdmin;
use Yii;

/**
 * 校区负责人
 * Class CampusAdminController
 * @package backend\controllers
 */
class CampusAdminController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = CampusAdmin::className();
    }

    /**
     * 首页
     * @return array|string
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex(true);
            $campus = array_column($data['data'],'campus');
            $campusAll = Campus::getCampusAll(['id'=>$campus],['id','name']);
            $campusAll = array_column($campusAll,'name','id');
            foreach ($data['data'] as &$datum){
                $datum['campus'] = $campusAll[$datum['campus']]??'';
            }
            return $data;
        }
        $url = Yii::$app->request->hostInfo.'/campus';
        $campus = Yii::$app->request->get('campus','');
        $campusAll = Campus::find()->where(['delete'=>0])->asArray()->all();
        return $this->render('',['campus'=>$campus,'campusAll'=>$campusAll,'url'=>$url]);
    }

    /**
     * 添加
     * @return array|string
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     */
    public function actionAdd()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $model = new CampusAdmin();
            if ($model->load($post, '') && $model->validate()) {
                if (!$model->realname) $model->realname = $model->username;
                if (!$model->password) {
                    $model->password = '123456';
                }
                $model->show_password = $model->password;
                $model->password = Yii::$app->security->generatePasswordHash($model->password);
                if ($model->save(false)) {
                    return commonApi::message('添加成功', true);
                }
            }
            return commonApi::message(commonApi::getModelError($model), false);
        }
        $campus = Yii::$app->request->get('campus','');
        $campusName = '';
        if ($campus){
            $campusData = Campus::getCampusAll(['id'=>$campus],['name']);
            $campusName = $campusData[0]['name']??'';
        }
        return $this->render('',['campus'=>$campus,'campusName'=>$campusName]);
    }

    /**
     * 所有校区树
     * @return array|string
     */
    public function actionTree(){
        if(Yii::$app->request->isPost){
            $data = Campus::getCampusAll(['status'=>1,'delete'=>0],['id','name']);
            foreach ($data as &$datum){
                $datum['parent_id'] = 0;
            }
            return commonApi::message('操作成功', true, $data);;
        }else{
            $id = Yii::$app->request->get('id', 0);
            $root = Yii::$app->request->get('root', 1);
            return $this->render('',['id'=>$id,'root'=>$root]);
        }
    }
}